package com.gree.scmservice.dao.saasDao;

import com.gree.common.entity.TablePageData;
import com.gree.common.utils.StringUtils;
import com.gree.scmservice.entity.saas.SaasCommodity;
import com.gree.scmservice.entity.saas.SaasSysAddon;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.entity.Record;
import org.nutz.dao.pager.Pager;
import org.nutz.dao.sql.Sql;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Repository
public class SaasAddonDao {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private Dao dao;

    public SaasSysAddon insertSaasSysAddon(SaasSysAddon saasSysAddon){
        SaasSysAddon insert = dao.insert(saasSysAddon);
        return insert;
    }

    public int deleteSaasSysAddonById(Long addonId){
        int delete = dao.clear(SaasSysAddon.class, Cnd.where("addon_id", "=", addonId));
        return delete;
    }

    public int deleteSaasSysAddonByIds(Long[] addonIds){
        int delete = dao.clear(SaasSysAddon.class, Cnd.where("addon_id", "in", addonIds));
        return delete;
    }

    public int updateSaasSysAddon(SaasSysAddon saasSysAddon){
        int update = dao.update(saasSysAddon);
        return update;
    }

    public int updateSaasAddonIcon(String addonId, String icon){
        return dao.update(SaasSysAddon.class, Chain.make("icon", icon), Cnd.where("addon_id","=",addonId));
    }


    public SaasSysAddon selectSaasSysAddonById(Long addonId){
        SaasSysAddon fetch = dao.fetch(SaasSysAddon.class, addonId);
        return fetch;
    }

    public TablePageData selectSaasSysAddonList(SaasSysAddon saasSysAddon){
        Pager pager = dao.createPager(saasSysAddon.getPageNum(), saasSysAddon.getPageSize());
        saasSysAddon.saasSysAddonNoPage();
        String author = saasSysAddon.getAuthor();
        saasSysAddon.setAuthor(null);
        String name = saasSysAddon.getName();
        saasSysAddon.setName(null);
        Cnd cnd = Cnd.from(dao,saasSysAddon);
        if(StringUtils.isNotEmpty(author)){
            cnd = cnd.and("username","like", "%"+author+"%");
        }
        if(StringUtils.isNotEmpty(name)){
            cnd = cnd.and("name","like", "%"+name+"%");
        }
        List query = dao.query(SaasSysAddon.class, cnd, pager);
        pager.setRecordCount(dao.count(SaasSysAddon.class, cnd));
        TablePageData tablePageData = new TablePageData(query, pager);
        return tablePageData;
    }
}
